package dao

import (
	"context"
	"sale-admin/internal/dal"

	"github.com/golang-module/carbon/v2"
	"gorm.io/gorm"
)

type WechatRate struct{}

type TotalPaidUser struct {
	PayNum int64 `json:"pay_num"`
}

// 支付人数
func (WechatRate) GetDailyRepeatUser(ctx context.Context, db *gorm.DB, robotName, goodsID, date string) (data TotalPaidUser, err error) {
	m := dal.Use(db).MaWechatRate
	tx := m.WithContext(ctx)
	if robotName != "" {
		tx = tx.Where(m.RobotName.Eq(robotName))
	}
	if goodsID != "" {
		tx = tx.Where(m.GoodsID.Eq(goodsID))
	}
	if date != "" {
		tx = tx.Where(m.AddTime.Eq(carbon.ParseByLayout(date, carbon.DateLayout).ToStdTime()))
	}
	tx.Select(m.UniqueID.Distinct().Count().As("pay_num")).
		Scan(&data)
	return
}
